<?php
/**
 * Created by PhpStorm.
 * User: N
 * Date: 2017/12/13
 * Time: 15:17
 */
namespace common\models;

use Yii;
use yii\base\Model;
use common\models\Models;
use backend\modules\content\models\Sitemodel;
use backend\modules\content\models\Modelfield;
use yii\db\ActiveRecord;
use yii\data\ActiveDataProvider;
use yii\data\Pagination;


/**
 * This is the model class for table "Otype".
 *
 * @property string $catid
 * @property string $siteid
 **/
class Otype extends ActiveRecord
{
     /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%otype}}';
    }


    public function getifshow($ifshow)
    {
        switch($ifshow){
            case 0:
                return '是';
                break;
            case -1:
                return  '否';
                break;
            default:
                return '未知';
        }
    }

    /**
    * 验证规则
    */
    public function rules()
    {
        return [
            [['type_name'],'required','message'=>'不能为空'],
            [['type_name','info'],'string','message'=>'格式不正确'],
            [['type_id','parentid','listorder','status'],'number','message'=>'格式不正确'],
        ];
    }

    public function beforeSave($insert){
        if($insert){
            //$this->if_show=1;
            //插入时补充操作
            $this->created_at = time();
            $this->updated_at = time();
        }else{
            //修改时补充操作
            $this->updated_at = time();
        }
      
        return true;

    }



        /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'type_name' => '分类名称',
            'parentid' => '上级分类id',
            'listorder' => '排序',
            'status' => '是否显示',
            'info'  => '分类说明'
        ];
    }


	/**
	 * 获取分类深度
	 * @param $id
	 * @param $array
	 * @param $i
	 */
	public static function get_level($id,$array=array(),$i=0) {
		foreach($array as $n=>$value){
			if($value['type_id'] == $id)
			{
				if($value['parentid']== '0') return $i;
				$i++;
				return self::get_level($value['parentid'],$array,$i);
			}
		}
	}


    


}